﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGym.BL;
using EGym.BL.DataGestor;
using EGym.BL.Forms;
using EGym.Data;
using EGym.Models;
using Trirand.Web.Mvc;

//Contorle do Grid
namespace EGym.Controllers
{
    public partial class AlunoController 
    {
        //public ActionResult GridAjax()
        //{
        //    // Get the model (setup) of the grid defined in the /Models folder.
        //    var gridModel = new AlunoModel();
        //    var grdMain = gridModel.AlunosGrid;

        //    // customize the default Orders grid model with custom settings
        //    // NOTE: you need to call this method in the action that fetches the data as well,
        //    // so that the models match
        //    SetUpGrid(grdMain);

        //    // Pass the custmomized grid model to the View
        //    return View(gridModel);
        //}

        // This method is called when the grid requests data
        public JsonResult DataRequested()
        {
            // Get both the grid Model and the data Model
            // The data model in our case is an autogenerated linq2sql database based on Northwind.
            AlunoGestor gestor = new AlunoGestor(new DBDataContext());
            
            var form = new AlunoForm();

            // customize the default Orders grid model with our custom settings
            SetUpGrid(form.GridMain);

            // return the result of the DataBind method, passing the datasource as a parameter
            // jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
            return form.GridMain.DataBind(gestor.GetAlunos(SessionManager.Usuario.AfiliadoId));
        }

        public void EditRows(AlunoForm form)
        {
            // Get the grid and database (northwind) models
            AlunoGestor gestor = new AlunoGestor(new DBDataContext());

            // Get the edit data using the JQGrid GetEditData() method
            var editData = form.GridMain.GetEditData();
            int alunoId = Convert.ToInt16(editData.RowData["alunoID"]);

            // If we are in "Edit" mode
            if (form.GridMain.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                // Get the data from and find the Order corresponding to the edited row
                Aluno aluno = gestor.GetAlunoById(Convert.ToInt32(editData.RowData["AfiliadoID"]),alunoId);

                // update the Order information
                aluno.DataNascimento = Convert.ToDateTime(editData.RowData["DataNascimento"]);
                aluno.AfiliadoId = Convert.ToInt32(editData.RowData["AfiliadoID"]);
                aluno.Nome = editData.RowData["nome"];

                gestor.Salvar();
            }
            if (form.GridMain.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                // since we are adding a new Order, create a new istance
                Aluno aluno = new Aluno();
                // set the new Order information
                aluno.DataNascimento = Convert.ToDateTime(editData.RowData["DataNascimento"]);
                aluno.Nome = editData.RowData["Nome"];

                gestor.AdcionarAlunoAssociadoComAfiliacaoAssociada(Convert.ToInt32(editData.RowData["AfiliadoId"]),aluno);
                gestor.Salvar();
            }
            if (form.GridMain.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                Aluno aluno = gestor.GetAlunoById(Convert.ToInt32(editData.RowData["AfiliadoID"]), alunoId);

                // delete the record                
                gestor.ExcluiAluno(aluno);
            }
        }

        private void SetUpGrid(JQGrid grdMain)
        {
            BaseSetupJQGrid(grdMain);
            // Customize/change some of the default settings for this model
            // ID is a mandatory field. Must by unique if you have several grids on one page.
            grdMain.ID = "grdAlunoMain";
            // Setting the DataUrl to an action (method) in the controller is required.
            // This action will return the data needed by the grid
            SetupJQGridUrl(grdMain);
            grdMain.Columns.Find(c => c.DataField == "Nome").Searchable = true;

            //SetShipNameSearchDropDown(grdMain);
            //// setup the dropdown values for the CustomerID editing dropdown
            //SetUpAfiliadoIdEditDropDown(grdMain);
        }

        private void SetupJQGridUrl(JQGrid grdMain)
        {
            grdMain.DataUrl = Url.Action("DataRequested");
            grdMain.EditUrl = Url.Action("EditRows");
        }

        private void SetShipNameSearchDropDown(JQGrid grdMain)
        {
            JQGridColumn nomeColumn = grdMain.Columns.Find(c => c.DataField == "Nome");
            nomeColumn.Searchable = true;
            nomeColumn.DataType = typeof(string);
            nomeColumn.SearchToolBarOperation = SearchOperation.Contains;
            nomeColumn.SearchType = SearchType.TextBox;
        }

        private void SetUpAfiliadoIdEditDropDown(JQGrid grdMain)
        {
            // setup the grid search criteria for the columns
            JQGridColumn customersColumn = grdMain.Columns.Find(c => c.DataField == "AfiliadoId");
            customersColumn.Editable = true;
            customersColumn.EditType = EditType.DropDown;

            // Populate the search dropdown only on initial request, in order to optimize performance
            if (grdMain.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                AfiliadoGestor gestor = new AfiliadoGestor(new DBDataContext());
                var editList = from reg in gestor.GetAfiliados()
                               select new SelectListItem
                               {
                                   Text = reg.Nome,
                                   Value = reg.AfiliadoId.ToString()
                               };

                customersColumn.EditList = editList.ToList<SelectListItem>();
            }
        }       
    }
}
